<template>
  <div class="dashboard-editor-container">
    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="24" style="text-align: right">
        <el-date-picker
      v-model="date"
      type="daterange"
      align="right"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
        value-format="yyyy-MM-dd"
      :picker-options="pickerOptions">
    </el-date-picker>
        <el-button style="margin-left: 20px" type="primary" @click="search"
          >搜索</el-button
        >
      </el-col>
    </el-row>
    <panel-group
      @handleSetLineChartData="handleSetLineChartData"
      :user-count="userCount"
      :train-count="trainCount"
      :train-time="trainTime"
      :pass-rate="passRate"
    />
    <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <bar-chart v-if="departmentData" :chart-data="departmentData" />
    </el-row>
    <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <line-chart :chart-data="lineChartData" />
    </el-row>
    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <pie-chart />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import PanelGroup from "./dashboard/PanelGroup";
import LineChart from "./dashboard/LineChart";
import RaddarChart from "./dashboard/RaddarChart";
import PieChart from "./dashboard/PieChart";
import BarChart from "./dashboard/BarChart";
import { getCardTitleData, getBarChartData } from "@/api/dashboard";
import moment from 'moment'; 
const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145],
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130],
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130],
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130],
  },
};

export default {
  name: "Index",
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
  },
  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      //人员总数
      userCount: 0,
      //培训次数
      trainCount: 0,
      //培训时长
      trainTime: 0,
      //合格率
      passRate: 0,
      departmentData: {
        xAxis: [],
        series: [],
      },
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      seacrchType: 1,
      date: ['2024-01-01', moment().endOf('year').format('YYYY-MM-DD')],
    };
  },
  created() {
    this.$nextTick(() => {
      this.getCardTitleData();
      this.getBarChartData();
    });
  },
  methods: {
    search() {
      console.log(this.date)
      this.getBarChartData();
    },
    getCardTitleData() {
      getCardTitleData({
        startTime: this.date[0],
        endTime:  this.date[1],
      }).then((response) => {
        this.userCount = response.data.userCount;
        this.trainCount = response.data.trainCount;
        this.trainTime = response.data.trainTime;
        this.passRate = +response.data.passRate;
      });
    },
    getBarChartData() {
      getBarChartData({
        startTime: this.date[0],
        endTime:  this.date[1],
        seacrchType: this.seacrchType,
      }).then((response) => {
        this.departmentData.xAxis = response.data.xAxis;
        this.departmentData.series = response.data.series;
      });
    },
    handleSetLineChartData(type) {
      this.seacrchType = type;
      this.getBarChartData();
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
